<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="mail">
      <el-input v-model="dataForm.mail" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="支付费用(只有租户需要支付费用)" prop="expense">
      <el-input v-model="dataForm.expense" placeholder="支付费用(只有租户需要支付费用)"></el-input>
    </el-form-item>
    <el-form-item label="其上级的id,管理员上级id为-1" prop="managerId">
      <el-input v-model="dataForm.managerId" placeholder="其上级的id,管理员上级id为-1"></el-input>
    </el-form-item>
    <el-form-item label="标记符(标记角色，包括用户，租户，管理员)" prop="sign">
      <el-input v-model="dataForm.sign" placeholder="标记符(标记角色，包括用户，租户，管理员)"></el-input>
    </el-form-item>
    <el-form-item label="0表示未删除，1表示删除" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="0表示未删除，1表示删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          telephone: '',
          mail: '',
          expense: '',
          managerId: '',
          sign: '',
          isDeleted: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          mail: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          expense: [
            { required: true, message: '支付费用(只有租户需要支付费用)不能为空', trigger: 'blur' }
          ],
          managerId: [
            { required: true, message: '其上级的id,管理员上级id为-1不能为空', trigger: 'blur' }
          ],
          sign: [
            { required: true, message: '标记符(标记角色，包括用户，租户，管理员)不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '0表示未删除，1表示删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.password = data.user.password
                this.dataForm.telephone = data.user.telephone
                this.dataForm.mail = data.user.mail
                this.dataForm.expense = data.user.expense
                this.dataForm.managerId = data.user.managerId
                this.dataForm.sign = data.user.sign
                this.dataForm.isDeleted = data.user.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'telephone': this.dataForm.telephone,
                'mail': this.dataForm.mail,
                'expense': this.dataForm.expense,
                'managerId': this.dataForm.managerId,
                'sign': this.dataForm.sign,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
